package com.atguigui.leetcode1;

/**
 * 2460.对数组执行操作
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/5 9:18
 */
public class P2460ApplyOperationsToAnArray {
    public static void main(String[] args) {
        Solution solution = new P2460ApplyOperationsToAnArray().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] applyOperations(int[] nums) {

            int n = nums.length;
            for (int i = 0; i < nums.length - 1; i++) {
                if (nums[i] == nums[i + 1]) {
                    nums[i] = nums[i] * 2;
                    nums[i + 1] = 0;
                    i += 1;
                }
            }

            int[] ans = new int[n];
            int index = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != 0) {
                    ans[index] = nums[i];
                    index++;
                }
            }
            return ans;
        }
    }
}
